package class01;

import java.util.Arrays;

/**
 * @author zhangchaoliang
 * create 2022
 */
public class e2 {
    public static boolean exit(int[] arr,int num){
        if (arr==null||arr.length==0){
            return false;
        }
        int L=0;
        int R=arr.length-1;
        int mid = 0;
        while(L<R){
            mid=L+((R-L)>>1);
            if (arr[mid]==num){
                return true;
            }else if (arr[mid]>num){
                R=mid-1;
            }else{
                L=mid+1;
            }
        }
        return arr[L]==num;
    }

    public static boolean test(int[] arr,int num){
        for (int cur:arr){
            if (cur==num)
                return true;
        }
        return false;
    }

    public static int[] RandomArrays(int maxSize,int maxValue){
        int[] arr = new int[(int)((maxSize+1)*Math.random())];
        for (int i=0;i<arr.length;i++){
            arr[i]=(int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());
        }
        return arr;
    }

    public static void main(String[] args) {
        int testTime = 500000;
        int maxSize = 100;
        int maxValue=100;
        boolean successed = true;

        for (int i =0;i<testTime;i++){
            int[] arr = RandomArrays(maxSize,maxValue);
            Arrays.sort(arr);
            int value = (int)((maxValue+1)*Math.random())-(int)((maxValue+1)*Math.random());
            if(exit(arr,value)!=test(arr,value)){
                successed=false;
                break;
            }
        }
        System.out.println(successed?"成功！":"失败！");
    }
}
